package com.service.jkt.impl;

import com.dao.*;
import com.entity.*;
import com.service.jkt.RegisterbookService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@org.springframework.stereotype.Service
public class RegisterbookImpl implements RegisterbookService {
    @Resource
    OwnershipDao ownershipDao;
    @Resource
    RoomDao roomDao;
    @Resource
    RegisterbookDao registerbookDao;
    @Resource
    HouseDao houseDao;
    @Resource
    BuildingDao buildingDao;
    @Resource
    ServiceDao serviceDao;
    @Override
    public void addRegisterbook( Integer serviceid, Integer uid, Integer clientid, Integer roomid) {

        Room room = roomDao.selectByPrimaryKey(roomid);
        Integer buildingid = room.getBuildingid();
        Building building = buildingDao.selectByPrimaryKey(buildingid);
        House house =houseDao.selectByPrimaryKey(building.getHid());
        OwnershipExample ownershipExample = new OwnershipExample();
        ownershipExample.createCriteria().andRoomidEqualTo(roomid);
        List<Ownership> ownerships = ownershipDao.selectByExample(ownershipExample);
        Integer ownershipid=null;
        if (ownerships.size()!=0){
            ownershipid=ownerships.get(0).getOsid();
        }

        Registerbook registerbook = new Registerbook();
        registerbook.setHid(house.getHid());
        registerbook.setRegisterbookno(randomIntegerNumber());
        registerbook.setServiceid(serviceid);
        registerbook.setUid(uid);
        registerbook.setBuildingid(buildingid);
        registerbook.setClientid(clientid);
        registerbook.setOsid(ownershipid);
        registerbook.setRoomid(roomid);
        registerbookDao.insertSelective(registerbook);
        Service service =new Service();
        service.setServiceid(serviceid);
        service.setServicestatusid(5);
        serviceDao.updateByPrimaryKeySelective(service);
    }

    public Integer randomIntegerNumber(){
        int i =  (int)(Math.random()*2)+100;
       Integer randomNum =  (int)(new Date().getTime()) + i ;
        return randomNum;
    }
}
